package isp.apps.example.shoppingcart.action;

import com.opensymphony.xwork.ActionContext;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.domain.CartFactory;
import isp.apps.example.shoppingcart.domain.ShoppingCart;

import java.util.Map;

public abstract class ShoppingAction
  extends ActionSupport
{
  private ShoppingCart myShoppingCart;

  private static final String CART_KEY = new String("cartkey");

  public ShoppingCart getCart()
  {
    if (myShoppingCart == null)
    {
      Map session = ActionContext.getContext().getSession();
      if (session.containsKey(CART_KEY))
        myShoppingCart = (ShoppingCart) session.get(CART_KEY);
      else
      {
        ShoppingCart sc = CartFactory.getCart();
        session.put(CART_KEY, sc);
        myShoppingCart = sc;
      }
    }
    return myShoppingCart;
  }
  
  public void newCart()
  {
    myShoppingCart = null;
    Map session = ActionContext.getContext().getSession();
    if (session.containsKey(CART_KEY))
      session.remove(CART_KEY);
  }


}
